﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Cashier.utils;
using Cashier_Utils;
using Cashier_Model;

namespace Cashier.pos
{
    public partial class UnPendingForm : Form
    {
        private PosForm posform;
        private Hashtable shipmentTable;
        private bool isaccount = false;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="posform"></param>
        /// <param name="shipmentTable"></param>
        public UnPendingForm(PosForm posform, Hashtable shipmentTable)
        {
            InitializeComponent();
            this.posform = posform;
            this.shipmentTable = shipmentTable;

            this.InitialDataGridViewColumns();
        }

        /// <summary>
        /// 初始化DataGridView列
        /// </summary>
        protected void InitialDataGridViewColumns()
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("shipmentno", "出货编号", true, 20, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("goods_name", "商品名称", true, 60, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("memberno", "客户编号", true, 20, ActKey.LEFT, ActKey.LEFT, true, false));

            if (this.shipmentTable != null && this.shipmentTable.Count > 0)
            {
                BillShipment billShipment = null;
                ArrayList list = null;
                IEnumerator iter = this.shipmentTable.Keys.GetEnumerator();
                while (iter.MoveNext())
                {
                    billShipment = iter.Current as BillShipment;
                    if (billShipment != null)
                    {
                        int rowIndex = this.dataGridView1.Rows.Add();
                        this.dataGridView1.Rows[rowIndex].Cells["shipmentno"].Value = billShipment.Shipmentno;
                        this.dataGridView1.Rows[rowIndex].Cells["memberno"].Value = billShipment.Memberno;
                        StringBuilder sb = new StringBuilder();
                        list = this.shipmentTable[billShipment] as ArrayList;
                        if (list != null && list.Count > 0)
                        {
                            foreach (BillShipmentGoods lopGoods in list)
                            {
                                sb.Append(lopGoods.GoodsName).Append(",");
                            }
                            sb.Remove(sb.Length - 1, 1);
                            this.dataGridView1.Rows[rowIndex].Cells["goods_name"].Value = sb.ToString();
                        }
                    }
                }
            }
        }

        /// <summary>
        /// OK事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_ok_Click(object sender, EventArgs e)
        {
            isaccount = true;
            this.Close();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_del_Click(object sender, EventArgs e)
        {
            String shipmentno = dataGridView1.SelectedRows[0].Cells["shipmentno"].Value.ToString();
            this.DeleteShipments(shipmentno);
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_cancel_Click(object sender, EventArgs e)
        {
            isaccount = false;
            this.Close();
        }

        /// <summary>
        /// 键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Escape:
                    isaccount = false;
                    this.Close();
                    break;
                case Keys.Delete:
                    String shipmentno = dataGridView1.SelectedRows[0].Cells["shipmentno"].Value.ToString();
                    this.DeleteShipments(shipmentno);
                    break;
                case Keys.Enter:
                    if (!button_ok.Focused)
                    {
                        isaccount = true;
                        this.Close();
                    }
                    break;
            }
            return b;
        }

        /// <summary>
        /// 删除挂单出货单
        /// </summary>
        protected void DeleteShipments(String shipmentno)
        {
            try
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {
                    DialogResult result = MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                        IEnumerator iter = this.shipmentTable.Keys.GetEnumerator();
                        while (iter.MoveNext())
                        {
                            BillShipment lopBillShipment = iter.Current as BillShipment;
                            if (lopBillShipment != null && lopBillShipment.Shipmentno.Equals(shipmentno))
                            {
                                this.shipmentTable.Remove(lopBillShipment);
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 窗口关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UnPendingForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isaccount)
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {
                    String shipmentno = dataGridView1.SelectedRows[0].Cells["shipmentno"].Value.ToString();
                    posform.UnPending(shipmentno);

                    //删除挂单商品
                    if (dataGridView1.SelectedRows.Count > 0)
                    {
                        dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                        IEnumerator iter = this.shipmentTable.Keys.GetEnumerator();
                        while (iter.MoveNext())
                        {
                            BillShipment lopBillShipment = iter.Current as BillShipment;
                            if (lopBillShipment != null && lopBillShipment.Shipmentno.Equals(shipmentno))
                            {
                                this.shipmentTable.Remove(lopBillShipment);
                                break;
                            }
                        }
                    }
                }
                else
                    e.Cancel = true;
            }
        }
    }
}
